<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2002 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
?>
<?php
// Hack prevention.
if (!empty($_REQUEST["GALLERY_BASEDIR"])) {
	print "Security violation\n";
	exit;
}
?>
<?php
require("./init.php");

// Hack check
if (!$gallery->user->canWriteToAlbum($gallery->album)) {
	exit;
}
?>


<html>
<head>
  <title><?php echo _XG_CUSTOM_THUMBNAIL; ?></title>
  <?php echo getStyleSheetLink() ?>
</head>

<?php 
// Get vars explicitly to suppress warnings
getRequestVars('action', false);

if ($action == "doit") {
	
	#-- rebuild the thumbnail, cropped) ---
	echo _XG_REMAKING_THUMBNAIL;
	my_flush();
	if ($gallery->session->albumName && isset($index)) { 
		$photo = $gallery->album->getPhoto($index);
		$photo->image->setThumbRectangle($crop_x, $crop_y, $crop_w, $crop_h);
		$photo->makeThumbnail($gallery->album->getAlbumDir(), $gallery->album->fields["thumb_size"]);
		$gallery->album->setPhoto($photo, $index);
		$gallery->album->save();
	}	
	
	#-- close and reload parent ---
	dismissAndReload();

} else if ($action == _XG_CANCEL) {
	#-- just close ---
	dismiss();
} else {
	#-- show the applet ---
?>
<body>

<span class="popuphead"><?php echo _XG_CUSTOM_THUMBNAIL;?></span>
<br>

<?php
	#-- are we a go? ---
	if ($gallery->session->albumName && isset($index)) { 

		$photo = $gallery->album->getPhoto($index);
	
		#-- the url to the image ---
		$photoURL = $gallery->album->getAlbumDirURL("highlight") . "/";
		if ($photo->image->resizedName) {
			$photoURL .= $photo->image->resizedName . "." . $photo->image->type;
		} else {
			$photoURL .= $photo->image->name . "." . $photo->image->type;
		}

		#-- the dimensions of the raw image ---
		list($image_w, $image_h) = $photo->image->getRawDimensions();
		list($t_x, $t_y, $t_w, $t_h) = $photo->image->getThumbRectangle();
	
		$bgcolor = "#FFFFFF";

		/* Build up the submit URL */
		if (isset($HTTP_SERVER_VARS['HTTPS']) && stristr($HTTP_SERVER_VARS['HTTPS'], "on")) {
		    $submit = "https://";
		} else {
		    $submit = "http://";
		}

		if (empty($HTTP_SERVER_VARS['REQUEST_URI'])) {
		    $submit .= $HTTP_SERVER_VARS['HTTP_HOST'];
		    $submit .= $HTTP_SERVER_VARS['PATH_INFO'];
		    $submit .= '?';
		    $submit .= $HTTP_SERVER_VARS['QUERY_STRING'];
		} else {
		    $submit .= $HTTP_SERVER_VARS['HTTP_HOST'];
		    $submit .= $HTTP_SERVER_VARS['REQUEST_URI'];
		}
?>

<span class="popup">
<?php echo _XG_CHOOSE_IMAGE_THUMNAIL;?>
</span>

<applet code="ImageCrop" width="460" height="430" codebase="<?php echo $GALLERY_URL?>java" archive="ImageTools.jar">
  <param name="type"   value="application/x-java-applet;version=1.1.2">
  <param name="bgcolor"  value="<?php echo $bgcolor ?>">
  <param name="image"    value="<?php echo $photoURL ?>">
  <param name="image_w"  value="<?php echo $image_w ?>">
  <param name="image_h"  value="<?php echo $image_h ?>">
  <param name="crop_x"   value="<?php echo $t_x ?>">
  <param name="crop_y"   value="<?php echo $t_y ?>">
  <param name="crop_w"   value="<?php echo $t_w ?>">
  <param name="crop_h"   value="<?php echo $t_h ?>">
  <param name="submit"   value="<?php echo $submit ?>">
  <param name="crop_to_size"  value="<?php echo $gallery->album->fields["thumb_size"] ?>">
</applet>

<?php 
    	#-- we're not a go. abort! abort! ---
	} else { 
		gallery_error(_XG_ERROR_NO_ALBUM_SELECT);
	} 
} 
?>
</body>
</html>